<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
nstd (The Non-Standard Go library)
</h1>
<p></p>
<div class="tmd-usual">
<code class="tmd-code-span">nstd</code> is a single-package go module which provides some missing types and functions in the standard library.
</div>
<p></p>
<div class="tmd-usual">
Project page: <a href="https://github.com/go101/nstd">https://github.com/go101/nstd</a>
</div>
<p></p>
<div class="tmd-usual">
Docs page: <a href="https://docs.go101.org/std/pkg/go101.org/nstd.html">https://docs.go101.org/std/pkg/go101.org/nstd.html</a>
</div>
<p></p>
<div class="tmd-usual">
Module path: <span class="tmd-bold"><span class="tmd-italic">go101.org/nstd</span></span>
</div>
<p></p>
<div class="tmd-usual">
Please follow <a href="https://twitter.com/zigo_101">@zigo_101</a> to get the latest news of <code class="tmd-code-span">nstd</code> (and all kinds of Go details/facts/tips/...).
</div>
<p></p>
<p></p>
<h2 class="tmd-header-2">
Some examples using <code class="tmd-code-span">nstd</code>
</h2>
<p></p>
<div class="tmd-usual">
Example 1:
</div>
<pre class="tmd-code">
<code class="language-Go">package main

import (
	"go101.org/nstd"
	"log"
)

const debug = true

const foo = "foo"

func debugPrint(s string) {
	if debug {
		log.Print(s)
	}
}

func main() {
	var bar = "bar"
	_ = debug &amp;&amp; nstd.Logf(foo + ":" + bar)
	// It is cleaner than
	//    if debug {
	//         log.Print(foo + " " + bar)
	//    }
	//
	// And it is more performant than
	//    debugPrint(foo + " " + bar)
	// when debug is false.
}
</code></pre>
<p></p>
<div class="tmd-usual">
Example 2:
</div>
<pre class="tmd-code">
<code class="language-Go">package main

import (
	"go101.org/nstd"
)

func ExampleMutexAndWaitGroup() {
	const N = 1000
	var n = 0
	defer func() {
		nstd.Printfln("n = %d", n) // will print a new line at the end
		if expected := N*7; n != expected {
			nstd.Panicf("n (%d) != %d", n, expected) // panic with format
		}
	}()

	var wg nstd.WaitGroup
	defer wg.Wait()

	var m nstd.Mutex
	for range [1000]struct{}{} {
		// WaitGroup.Go starts several tasks.
		wg.Go(func() {
			defer m.Lock().Unlock() // call Lock and Unlock chainly
			n += 2
		}, func() {
			defer m.Lock().Unlock()
			n += 1
		})
		
		// WaitGroup.GoN starts one task several times.
		wg.GoN(3, func() {
			defer m.Lock().Unlock()
			n += 1
		})
	}

	// Mutex.Do guards the execution of a function.
	m.Do(func() {
		n += N
	})
}

func main() {
	ExampleMutexAndWaitGroup()
}
</code></pre>
<p></p>
<div class="tmd-usual">
Example 3:
</div>
<pre class="tmd-code">
<code class="language-Go">package main

import (
	"go101.org/nstd"
)

func main() {
	pInt := nstd.New(123)
	nstd.Printfln("%T: %v", pInt, *pInt) // *int: 123
	pBool := nstd.New(true)
	nstd.Printfln("%T: %v", pBool, *pBool) // *bool: true

	nstd.Printfln("%v", nstd.ZeroOf[int]())  // 0
	nstd.Printfln("%v", nstd.ZeroOf[bool]()) // false

	nstd.Zero(pInt)
	nstd.Printfln("%v", *pInt) // 0
	nstd.Zero(pBool)
	nstd.Printfln("%v", *pBool) // false
}
</code></pre>
<p></p>
<div class="tmd-usual">
Example 4:
</div>
<pre class="tmd-code">
<code class="language-Go">package main

import (
	"go101.org/nstd"
)

func main() {
	var aCondition = true
	
	var m = map[string]int{"Zig": 2016}
	_ = aCondition &amp;&amp; nstd.HasEntry(m, "Go")
	// So that, no need to write it in two lines:
	//     _, ok := m["Go"]
	//     _ = aCondition &amp;&amp; ok
	
	var x any = 123
	var n int
	_ = aCondition &amp;&amp; nstd.TypeAssert(x, &amp;n)
	// So that, no need to write it in two lines:
	//     n, ok := x.(int)
	//      _ = aCondition &amp;&amp; ok
	nstd.Printfln("n = %v", n) // n = 123
	_ = aCondition &amp;&amp; nstd.TypeAssert[int](x, nil)
	_ = aCondition &amp;&amp; nstd.TypeAssert(x, (*int)(nil))
}
</code></pre>
<p></p>
<div class="tmd-usual">
Example 5:
</div>
<pre class="tmd-code">
<code class="language-go">package main

import (
	"reflect"
	"go101.org/nstd"
)

func main() {
	var n int = 123
	var x any = n
	
	nstd.Printfln("type of x: %s", reflect.TypeOf(x)) // type of x: int
	nstd.Printfln("type of x: %s", nstd.TypeOf(x))    // type of x: interface {}
	
	nstd.Printfln("type of x: %s", reflect.ValueOf(x).Type()) // type of x: int
	nstd.Printfln("type of x: %s", nstd.ValueOf(x).Type())    // type of x: interface {}
}
</code></pre>
<p></p>
</div>
